﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;

using Telerik.Windows.Controls;

namespace Baosight.ZNH.ZM.UI.Controls
{
    public partial class SimpleCheckBoxGroup : UserControl
    {
        #region 变量定义

        private IList<CheckBox> checkedBoxes = new List<CheckBox>();
        private IList<CheckBox> allCheckBoxes = new List<CheckBox>();
        private IList<CheckBox> allCheckBox = new List<CheckBox>();
        private const int fontSizeNumber = 14;
        public int ColumnNumber { get; set; }

        public IList<CheckBox> checkedItems
        {
            get
            {
                return this.checkedBoxes;
            }
        }

        #endregion

        public SimpleCheckBoxGroup()
        {
            InitializeComponent();
            ColumnNumber = 5;
        }

        #region 初始化控件

        public void Init(IList<EIRow> dataList, string index)
        {
            checkedBoxes.Clear();
            this.checkGrid.Children.Clear();

            if (dataList.Count > 0)
            {
                BindItemsToGrid(this.checkGrid,dataList,index);
            }
            else
            {
                return;
            }
        }

        private void BindItemsToGrid(Grid gridName, IList<EIRow> list, string index)
        {
            gridName.Children.Clear();

            int count = list.Count;

            if (count > 0)
            {
                if (count > 4)
                {
                    if (count % 4 == 0)     //每行保存4个条目
                    {
                        DivRowAndColumn(gridName, count / 4, ColumnNumber);    //条目数为4的整数倍
                    }
                    else
                    {
                        DivRowAndColumn(gridName, count / 4 + 1, ColumnNumber);    //条目数不为4的整数倍，多添加一行
                    }
                }
                else        //默认划分为1行4列
                {
                    DivRowAndColumn(gridName, 1, ColumnNumber);
                }

                AddItemsToGrid(gridName, list, index);
            }
            else
            {
                return;
            }
        }

        private void DivRowAndColumn(Grid gridName, int rowCount, int colCount)
        {
            if (rowCount <= 0 || colCount <= 0)
            {
                rowCount = 1;
                colCount = 1;
            }
            else
            {
                for (int i = 0; i < rowCount; i++)
                {
                    gridName.RowDefinitions.Insert(i, new RowDefinition() { Height = GridLength.Auto });
                }
                for (int j = 0; j < colCount; j++)
                {
                    gridName.ColumnDefinitions.Insert(j, new ColumnDefinition() { Width = GridLength.Auto });
                }
            }
        }

        private void AddItemsToGrid(Grid gridName, IList<EIRow> list, string index)
        {
            int rowIndex = 0;
            int colIndex = 0;

            allCheckBoxes.Clear();
            allCheckBox.Clear();
            StackPanel stackAll = new StackPanel()
            {
                Orientation = Orientation.Horizontal,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Stretch,
                Margin = new Thickness(5, 0, 5, 0)
            };
            CheckBox checkAll = new CheckBox() { DataContext = "-1", Tag = "", Margin = new Thickness(5, 0, 5, 0) };
            checkAll.IsChecked = false;
            checkAll.Click += new RoutedEventHandler(CheckButtonItemClick);
            TextBlock textAll = new TextBlock() { Text = "全部", FontSize = fontSizeNumber };
            stackAll.Children.Add(textAll);
            stackAll.Children.Add(checkAll);

            allCheckBox.Add(checkAll);

            stackAll.SetValue(Grid.RowProperty, rowIndex);
            stackAll.SetValue(Grid.ColumnProperty, colIndex++);

            gridName.Children.Add(stackAll);
            for (int i = 0; i < list.Count; i++)
            {
                StackPanel stack = new StackPanel()
                {
                    Orientation = Orientation.Horizontal,
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                    VerticalAlignment = VerticalAlignment.Stretch,
                    Margin=new Thickness(5,0,5,0)
                };
                CheckBox check = new CheckBox() { DataContext = list[i], Tag = "" ,Margin=new Thickness(5,0,5,0)};
                check.IsChecked = false;
                check.Click += new RoutedEventHandler(CheckButtonItemClick);
                check.Checked += new RoutedEventHandler(CheckButtonItemChecked);
                TextBlock text = new TextBlock() { Text = list[i][index].ToString(), FontSize = fontSizeNumber };
     
                stack.Children.Add(text);
                stack.Children.Add(check);

                allCheckBoxes.Add(check);
                allCheckBox.Add(check);
                stack.SetValue(Grid.RowProperty, rowIndex);
                stack.SetValue(Grid.ColumnProperty, colIndex++);

                gridName.Children.Add(stack);

                if (colIndex == ColumnNumber)
                {
                    colIndex = 0; rowIndex++;
                }
            }
        }
        //后台取到的block填充的list和动态生成的list进行比对，确定是否勾选
        public void CheckItems(IList<EIRow> list)
        {
            foreach (var item in allCheckBoxes)
            {
                EIRow rowItem = item.DataContext as EIRow;
                foreach (var item2 in list)
                {
                    if (rowItem["fdKeyId"] != null && item2["fdKeyId"] != null)
                    {
                        if (rowItem["fdKeyId"].ToString().Equals(item2["fdKeyId"].ToString()))
                        {
                            item.IsChecked = true;
                            checkedBoxes.Add(item);
                        }
                    }
                }
            }
        }

        private void CheckButtonItemClick(object sender, RoutedEventArgs e)
        {
            CheckBox selectedBox = sender as CheckBox;

            if (selectedBox == null)
            {
                return;
            }
            if (selectedBox.DataContext.ToString().Equals("-1"))
            {
                IsOtherCheckBox(selectedBox);
            }
            else
            {
                if (selectedBox.IsChecked == false)
                {
                    checkedBoxes.Remove(selectedBox);
                }
                else
                {
                    checkedBoxes.Add(selectedBox);
                }
            }
            IsAllCheck(selectedBox);
        }

        private void CheckButtonItemChecked(object sender, RoutedEventArgs e)
        {
            CheckBox  allCheckBox = sender as CheckBox;
            IsAllCheck(allCheckBox);
           
        }

        private void IsOtherCheckBox(CheckBox cbCheck)
        {
            if (cbCheck.IsChecked == true)
            {
                for (int i = 1; i < allCheckBox.Count; i++)
                {
                    allCheckBox[i].IsChecked = true;
                    checkedBoxes.Add(allCheckBox[i]);
                }
            }
            else
            {
                for (int i = 1; i < allCheckBox.Count; i++)
                {
                    allCheckBox[i].IsChecked = false;
                    checkedBoxes.Remove(allCheckBox[i]);
                }
            }

        }

        private void IsAllCheck(CheckBox cbCheck)
        {
            if (cbCheck.IsChecked == true)
            {
                for (int i = 0; i < allCheckBox.Count; i++)
                {
                    if (allCheckBox[i].IsChecked == false)
                    {
                        FindObjectByData(allCheckBox, "-1").IsChecked = null;
                        return;
                    }

                }
                FindObjectByData(allCheckBox, "-1").IsChecked = true;
            }
            else
            {
                for (int i = 0; i < allCheckBox.Count; i++)
                {
                    if (allCheckBox[i].IsChecked == true)
                    {
                        FindObjectByData(allCheckBox, "-1").IsChecked = null;
                        return;
                    }

                }
                FindObjectByData(allCheckBox, "-1").IsChecked = false;
            }
        }

        private CheckBox FindObjectByData(IList<CheckBox> checkSelected, string strTypeName)
        {
            for (int i = 0; i < checkSelected.Count; i++)
            {
                if (checkSelected[i].DataContext.ToString().Equals(strTypeName))
                    return checkSelected[i];
            }
            return null;
        }

        #endregion
    }
}
